<?php

/*
 * To change$this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Basic function for DB managment
 *
 * @author maxmin
 */
 class DBManager {
    private $_connection = null;
    private $_user='';
    private $_passWord='';
    private $_server='';
    private $_DBName='';
    private $_connected = false;
    private $_SQLite = false;
    
     function __destruct()
    {
        /* @var $mysql_close type */
          if ($this->_connected == true)   
          {
              mysql_close($this->_connection);
          }
    }
    private function contructSQLite($DBFile)
    {
        $this->_DBName = $DBFile;
        $this->_SQLite = true;
        $sqlite_error = '';
        $this->_connection = sqlite_open($DBFile, 0666, $sqlite_error);
        //mysql_connect("localhost","peter","abc123");
        if (!$this->_connection)
          {
           $this->_connected = false;
            echo('Could not connect: ' . $sqlite_error);
          }
          else
          {
            $this->_connected = true; 
          }
              
        
    }
    
    function __contruct($DBName, $IsSQLite=false,$User='',$PassWord='',$Server='')
    {
       $this->_DBName = $DBName;
       $this->_user = $User;
       $this->_passWord= $PassWord;
       $this->_server=$Server;
        if ($IsSQLite)
        {
            $this->contructSQLite($DBName);
            return;
            
        }
       $this->_connection = mysql_connect($this->_server,$this->_user,$this->_passWord);
        if (!$this->_connection)
          {
           $this->_connected = false;
            echo('Could not connect: ' . mysql_error());
          }
          else
          {
            $this->_connected = true; 
          }
              
        
    }
    public function ExeQuery($Query)
    {
        if ($this->_connected == false) return false;
        
        if ($this->_SQLite)
        {
           sqlite_query($this->_connection, $Query); 
           return true;
        }
            
        mysql_select_db($this->_DBName,$this->_connection);
        
        if (!mysql_query($Query,$this->_connection))
        {
            echo('MySql Error: ' . mysql_error());
        }
        return true;
    }
    function SelectQuery($Query)
    {
         if ($this->_connected == false) return false;
         
          if ($this->_SQLite)
        {
           $result2 =  sqlite_query($this->_connection, $Query); 
           return $result2;
        }
         
          mysql_select_db($this->_DBName,$this->_connection);
          $result = mysql_query($Query);
          return $result;
    
    
    }

}

?>
